package com.androidyuan.androidscreenshot_sysapi;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Build;
import android.os.Handler;
import android.support.annotation.RequiresApi;
import android.util.Log;
import android.view.View;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;

import com.androidyuan.androidscreenshot_sysapi.utils.BaseAccessibilityService;

import java.util.List;
import java.util.Timer;


public class AlipayAccessibilityService extends BaseAccessibilityService {
    public final String TAG = "AlipayService";
    private String money="22.2";
    private String moneContent="pfkj";
    private boolean flag = false;
    private int inputcount = 0;
    private Context context=null;

    @TargetApi(Build.VERSION_CODES.M)
    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    @Override
    public void onAccessibilityEvent(AccessibilityEvent event) {
        int eventType = event.getEventType();
        int i = event.getAction();
//        Log.i(TAG, (String) event.getClassName());
//        Log.i(TAG,event.toString());
        AccessibilityNodeInfo nodeInfo = getRootInActiveWindow();
        switch (eventType) {
            case AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED:
                AlipayViewClick(nodeInfo);
                break;
            case AccessibilityEvent.TYPE_VIEW_CLICKED:
                break;
            case AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED:
                break;
            default:
                AlipayViewClick(nodeInfo);
                break;
        }
    }
    protected void onServiceConnected(){

    }

    /***
     *  自动化获取二维码过程
     *  按键过程
     * @param node
     */
    @RequiresApi(api = Build.VERSION_CODES.M)
    private void AlipayViewClick(AccessibilityNodeInfo node) {
        List<AccessibilityNodeInfo> getMoney = node.findAccessibilityNodeInfosByText("收钱");
        List<AccessibilityNodeInfo> setMoneyNum = node.findAccessibilityNodeInfosByText("设置金额");
        List<AccessibilityNodeInfo> addGetReason = node.findAccessibilityNodeInfosByText("添加收款理由");
        List<AccessibilityNodeInfo> moneyRight = node.findAccessibilityNodeInfosByText("确定");
        //List<>
        //点击 收钱
        if (getMoney != null && getMoney.size() > 0) {
            performViewClick(getMoney.get(0));
        }
        //点击 设置金额
        if (setMoneyNum != null && setMoneyNum.size() > 0 && addGetReason.size() == 0) {
            if (setMoneyNum.get(0).isClickable()) {
                performViewClick(setMoneyNum.get(0));
            }
        }
        //标记 获取到两个文本框开始输入
        if (flag) {
            DFSinputData(node);
            performViewClick(moneyRight.get(0));
            flag = false;
            inputcount = 0;
            startService(new Intent(this, ScreenShotService.class));
            /*new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    Intent i = new Intent("androidyuan.shotter");
                    //这个不是必需的
                    i.addCategory(Intent.CATEGORY_DEFAULT);
                    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(i);
                }
            }, 200);//这里留足够的时间切换到别的app*/

        }
        //点击 添加收款理由
        if (addGetReason != null && addGetReason.size() > 0) {
            performViewClick(addGetReason.get(0));
            flag = true;
        }
    }


    /***
     *  遍历界面中的两个文本框，输入金额以及描述
     * @param node
     */
    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
    private void DFSinputData(AccessibilityNodeInfo node) {
        for (int i = 0; i < node.getChildCount(); i++) {
            if (node.getChild(i).isEditable()) {
                inputcount++;
                inputText(node.getChild(i), "123");
                continue;
            }
            if (node.getChild(i).getChildCount() > 0)
                DFSinputData(node.getChild(i));
        }
    }

    /**
     * 对View进行量测，布局后截图
     *
     * @param view
     * @return
     */
    public Bitmap convertViewToBitmap(View view) {
        //Application.class.
        view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
        view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
        view.setDrawingCacheEnabled(true);
        view.buildDrawingCache();
        Bitmap bitmap = view.getDrawingCache();
        return bitmap;
    }

    /******************************************************************************************************************************/
    @Override
    public void onInterrupt() {

    }

    /***
     * 遍历当前界面中所有控件的全部信息
     * @param node
     */
    private void DFSsearch(AccessibilityNodeInfo node) {
        for (int i = 0; i < node.getChildCount(); i++) {
            Log.i(Timer.class.toString(), node.getChild(i).toString());
            if (node.getChild(i).getChildCount() > 0) {
                DFSsearch(node.getChild(i));
            }

        }
    }
}
